<?php

/**
* OpenId Login class
*/

require_once 'openid.php';

class OpenIdLogin
{
	const GOOGLE_IDENTITY = 'https://www.google.com/accounts/o8/id';
	
	private $_openid = null;
	private $_id = '';
	
	function __construct($identity = OpenIdLogin::GOOGLE_IDENTITY)
	{
		$this->_openid = new LightOpenID;
		$this->_openid->identity = $identity;
	}
	
	public function getOpenId()
	{
		return $this->_openid;
	}
	
	public function login()
	{
		try
		{
			if (!$this->_openid->mode)
			{	        
				header('Location: ' . $this->_openid->authUrl());
			}
			elseif ($this->_openid->mode != 'cancel')
			{
				if ($this->_openid->validate())
				{
					$this->_id = $this->_openid->identity;
					return TRUE;
				}
			}
		}
		catch(ErrorException $e)	
		{
			return FALSE;
		}
		return FALSE;
	}
	
	public function getId()
	{
		return $this->_id;
	}
	
	public function __destruct()
	{
		unset($this->_openid);
		unset($this->_id);
	}
	
	public function __tostring()
	{
		return $this->getId();
	}
}
// 
// $openid = new OpenIdLogin();
// $openid->login();
// echo (string)$openid;

?>